<?php
namespace app\console\controller;

use think\File;
use think\Image;
use think\Cache;
use think\Request;
use think\response\Json;
use think\Controller;

/**
 * @作者 刘仲凯
 * 上传头像
 * @2017年2月23日
 */
class Upload extends Controller{

	public function upload(){

		$file = Request::instance()->file('file');
		$info = $file->getInfo();

		if($file->checkImg() !== true ){
			$arr = array(
					'code'=>'1',
					'error'=>'图片格式不正确',
				);
			return json($arr);die();
		}

		if($info['size']/1024 > 500){
			$arr = array(
					'code'=>'1',
					'error'=>'图片超出指定大小',
				);
			return json($arr);die();
		}

		$path = ROOT_PATH .'public/uploads/image/'.date('Ymd');
		$savename = "thumb_".uniqid().time().rand(100000,999999).".jpg";
		$result = $file->rule('date')->move($path,$savename);
		$pic = 'uploads/image/'.date('Ymd').'/'.$result->getFilename();

		$arr = array(
				'code'=>'0',
				'img'=>$pic,
			);
		return json($arr);die();
	}

	public function excel(){

		$file = Request::instance()->file('file');
		$info = $file->getInfo();

		$path = ROOT_PATH .'public/uploads/execl/';
		$savename = "execl_".uniqid().time().".xlsx";

		$result = $file->rule('date')->move($path,$savename);
		$filename = $path.$result->getFilename();

		$arr = ['code'=>'0','img'=>$filename];

		return json($arr);die();
	}
	public function uploads()
	{
		$pic='';
		$files = Request::instance()->file('file');
		$pic=[];
		foreach ($files as $file) {
			$info = $file->getInfo();

			if ($file->checkImg() !== true) {
				$arr = array(
					'code' => '1',
					'error' => '图片格式不正确',
				);
				return json($arr);
				die();
			}

			if ($info['size'] / 1024 > 500) {
				$arr = array(
					'code' => '1',
					'error' => '图片超出指定大小',
				);
				return json($arr);
				die();
			}

			$path = ROOT_PATH . 'public/uploads/image/' . date('Ymd');
			$savename = "thumb_" . uniqid() . time() . ".jpg";
			$result = $file->rule('date')->move($path, $savename);
			$pic = 'uploads/image/' . date('Ymd') . '/' . $result->getFilename();
		}
		$arr = array(
		'code' => '0',
		'img' => $pic,
	);
		return json($arr);
		die();
	}

}